/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sessionBeans;

import Entities.Users;
import Entities.Users_;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author xespin
 */
@Stateful
@LocalBean
public class loginSessionBean implements loginSessionBeanLocal {
    @PersistenceContext
    private EntityManager m_entityManager;
    
    private Users m_userEJB;
    
    
    public String createUser(String login, String password){
        Users user = getUser(login, password);
        
        if(user == null){
            m_userEJB = new Users();
            m_userEJB.setId(Long.valueOf(0));
            m_userEJB.setLogin(login);
            m_userEJB.setPassword(password);
            m_entityManager.persist(m_userEJB);  
            m_entityManager.flush();
            
            return "main";
        }else{
            return "error";
        }
        
    }
    
    /**
     * Check if the user exists in the database
     * @param login
     * @param password
     * @return 
     */
    public String checkUser(String login, String password){
        Users user = getUser(login, password);
        
        if(user == null){
            return "register";
        }else{
            return "main";
        }
    }
    
    /**
     * Gets the user if it exits
     * @param login
     * @param password
     * @return 
     */
    public Users getUser(String login, String password){
        CriteriaBuilder cBuilder = m_entityManager.getCriteriaBuilder();
        CriteriaQuery<Users> query = cBuilder.createQuery(Users.class);
        Root<Users> root = query.from(Users.class);
        query.where(cBuilder.and(cBuilder.equal(root.get(Users_.login),login),
                cBuilder.equal(root.get(Users_.password), password)));
        query.select(root);
        
//        Query query2 = 
//                m_entityManager.createQuery("from Users u where u.login = :loginStr and u.password = :mdp");
//        
//        query2.setParameter("loginStr", login);
//        query2.setParameter("mdp", password);
      
//        Users user = query2.getResultList().get(0);
        try{
            Users user = m_entityManager.createQuery(query).getResultList().get(0);
            return user;
        }catch(IndexOutOfBoundsException ex){
            System.out.println(ex.getMessage());
            return null;
        }   
        
    }
    
    /**
     * Gets the user without password information
     * @param login
     * @return 
     */
    public Users getUser(String login){
        CriteriaBuilder cBuilder = m_entityManager.getCriteriaBuilder();
        CriteriaQuery<Users> query = cBuilder.createQuery(Users.class);
        Root<Users> root = query.from(Users.class);
        query.where(cBuilder.equal(root.get(Users_.login),login));
        query.select(root);

        try{
            Users user = m_entityManager.createQuery(query).getResultList().get(0);
            return user;
        }catch(IndexOutOfBoundsException ex){
            System.out.println(ex.getMessage());
            return null;
        }   
        
    }
    
    /**
     * Get the ID of a user based on his login
     * @param login
     * @return 
     */
    public Long getReceiverId(String login){
       
        CriteriaBuilder cBuilder = m_entityManager.getCriteriaBuilder();
        CriteriaQuery<Users> query = cBuilder.createQuery(Users.class);
        Root<Users> root = query.from(Users.class);
        query.where(cBuilder.equal(root.get(Users_.login),login));       
        query.select(root);
        
        try{
            Users user = m_entityManager.createQuery(query).getResultList().get(0);
            this.m_userEJB = user;
            return user.getId();
        }catch(IndexOutOfBoundsException ex){
            System.out.println(ex.getMessage());
            return Long.valueOf(-1);
        } 
    }
    
    /**
     * teminates the session
     */
    public void terminate(){
        try {
            this.finalize();
        } catch (Throwable ex) {
            Logger.getLogger(loginSessionBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
    }
    
    
}
